//
//  WXPRegisterAgreementVC.m
//  MexicoLoan
//
//  Created by wxp on 2025/2/26.
//

#import "WXPRegisterAgreementVC.h"

@interface WXPRegisterAgreementVC ()<WKUIDelegate,WKNavigationDelegate>

@end

@implementation WXPRegisterAgreementVC

#pragma mark - lazy
- (WXPNavView *)navView{
    if (!_navView) {
        _navView = [WXPNavView new];
        [_navView resetViewWithTitle:@"Acuerdo de Servicio"];
    }
    return _navView;
}
- (WKWebView *)webView{
    if (_webView == nil) {
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        WKWebViewConfiguration *webViewConfiguration = [[WKWebViewConfiguration alloc] init];
        WKPreferences *preferences = [[WKPreferences alloc] init];
        preferences.javaScriptEnabled = YES;
        webViewConfiguration.preferences = preferences;
        WKWebsiteDataStore *dataStore = [WKWebsiteDataStore defaultDataStore];
        webViewConfiguration.websiteDataStore = dataStore;
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, 0, 0) configuration:webViewConfiguration];
        _webView.frame = CGRectMake(0, [GlobalMethod getNavgationBarHeight], SCREEN_WIDTH, SCREEN_HEIGHT-[GlobalMethod getNavgationBarHeight]);
        _webView.scrollView.bounces = NO;
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    return  _webView;
}

#pragma mark - viewDidLoad
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navView.leftTop = XY(0, 0);
    [self.view addSubview:self.navView];
    [self.tableView removeFromSuperview];
    
    [self.view addSubview:self.webView];
    
    [self loadWebView];
    
    self.navView.btnService.hidden = !isStr([GlobalData sharedInstance].Global_Token);
}

- (void)loadWebView{
    // 打点
    [GlobalMethod hitPointWithPageName:KEY_Agreement action:KEY_Enter extInfo:@{@"type": @"Register"}];
    NSString * html = [NSString stringWithFormat:@"%@/contract/register.html",DOMAIN_NAME];
    [self.webView loadRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:html]]];
}

#pragma mark - WKNavigationDelegate
// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    // 显示加载指示器
    [self showLoadingView];
}

// 页面加载完成时调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    // 隐藏加载指示器
    [self.loadingView hideLoading];
}

// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation withError:(NSError *)error {
    NSLog(@"WebView load error: %@", error.localizedDescription);
    // 隐藏加载指示器
    [self.loadingView hideLoading];
}

@end
